package com.cow.sram.module.archives;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.core.exception.BusinessException;
import com.cow.framework.utils.reflection.BeanToMapUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.comm.config.constant.MessageConstants;
import com.cow.sram.data.dao.UserinfoDAO;
import com.cow.sram.data.entity.Userinfo;
import com.cow.sram.data.mapper.ResourceinfoMapper;
import com.cow.sram.data.mapper.UserInfoMapper;
import com.cow.support.dto.User;
import com.github.pagehelper.PageHelper;

/**
 * 用户管理
 * 
 * @author Administrator
 *
 */
@Service
public class UserInfoService extends BaseComponent {

	@Autowired
	UserinfoDAO userinfoDao;
	@Autowired
	UserInfoMapper userInfoMapper;
	@Autowired
	OrgInfoService orgInfoService;
	@Autowired
	ResourceinfoMapper resourceinfoMapper;

	/** 查询 */
	public List<Map> queryAlluserInfo(Map parmMap) {
		if (parmMap.get("page") != null && parmMap.get("size") != null) {
			PageHelper.startPage((Integer) parmMap.get("page"), (Integer) parmMap.get("size"));
		}

		// 设置控制数据权限参数
		parmMap.put("userId", super.getCurUserId());
		com.github.pagehelper.Page<Userinfo> list = userInfoMapper.queryUserinfo(parmMap);
		com.github.pagehelper.Page<Map> resultList = new com.github.pagehelper.Page<Map>();
		BeanUtils.copyProperties(list, resultList);
		for (Userinfo userInfo : list) {
			resultList.add(convertToMap(userInfo));
		}
		return resultList;		
	}

	//转译
	private Map convertToMap(Userinfo userinfo) {
		Map dictMap = new HashMap();
		dictMap.put("authlevel", "AUTHLEVEL");
		dictMap.put("lockflag", "LOCKFLAG");
		dictMap.put("job", "job");
		Map map = BeanToMapUtil.convertBean(userinfo);
		//组织名称
		map.put("orgName", orgInfoService.getOrgById((Integer)map.get("orgid")).getOrgname());
		//创建人名字
		User creator = getUserById(userinfo.getCreator());
		if(creator==null){
			map.put("creatorName", "");
		}else{
			map.put("creatorName", creator.getUsername());
		}
		//查询上级组织编号
		Map parmMap = new HashMap();
		parmMap.put("pNodeId", userinfo.getOrgid());
		String orgids = resourceinfoMapper.getParentList(parmMap);
		orgids = orgids + "," + userinfo.getOrgid();
		map.put("orgids", orgids);
		dictUtil.valueToName(map, dictMap);
		return map;
	}

	/** 根据ID查询记录 */
	public Userinfo getUserInfoByID(Integer id) {
		return userinfoDao.findOne(id);
	}
	
	/** 根据帐号查询记录 */
	public Userinfo getUserInfoByAccountname(String accountname) {
		return userinfoDao.findByAccountname(accountname);
	}
	
	/** 根据ID查询记录 */
	public Map getUserMapByID(Integer id) {
		//转译
		return convertToMap(userinfoDao.findOne(id));
		
	}
	
	/**根据Id查询用户信息*/
	public User getUserById(Integer id){
		User user = (User)redisUtil.get("user-"+id);
		if(user == null){
			user = super.getAuthClient().getUserById(Long.valueOf(id));	
			
			//写缓存
			redisUtil.set("user-"+id,user,Long.valueOf(parameterUtil.get("userExpireTime")));
		}		
		return user;
	}	

	/** 新增用户 */
	public Userinfo saveUserInfo(Userinfo us) {
		if(super.getAuthClient().loadUserByAccountName(us.getAccountname()) != null){
			throw new BusinessException("B10014",MessageConstants.B10014);
		}else{
			return userinfoDao.save(us);
		}

	}
	public Userinfo editUserInfo(Userinfo us) {
		return userinfoDao.save(us);
	}
		
	

	/** 删除用户 */
	public void delUserInfo(Integer id) {
		Userinfo user = userinfoDao.findOne(id);
		userinfoDao.delete(user);
	}

}
